package com.dushiguang.findhoney.Utils.DataStorage.Db;

import android.content.Context;
import android.database.Cursor;
import android.text.TextUtils;

import com.dushiguang.findhoney.Key.Config;
import com.lidroid.xutils.DbUtils;
import com.lidroid.xutils.db.annotation.Table;
import com.lidroid.xutils.db.sqlite.SqlInfo;
import com.lidroid.xutils.db.sqlite.WhereBuilder;
import com.lidroid.xutils.exception.DbException;


/**
 * 初始化第三框架里的数据库操作
 * Created by ex-dushiguang201 on 2015-11-23.
 */
public class DbUtil {
    DbUtils db;

    public DbUtil(Context context){
        DbUtils.DaoConfig daoConfig = new DbUtils.DaoConfig(context);
        daoConfig.setDbName(Config.DBNAME);
        daoConfig.setDbVersion(Config.DBVERSION);

        this.db = DbUtils.create(daoConfig);
    }

    public DbUtils getDb() {
        if(db != null) {
            return db;
        }
        return null;
    }

    public String getCatchData(String data_flag) {
        DbUtils dbUtils = db;
        String result = "";
        Cursor cursor = null;
        String cachedData = null;

        try {
            dbUtils.createTableIfNotExist(ConfigDataCacheBean.class);

            cursor = dbUtils.execQuery(new SqlInfo("select server_ver,local_ver,cache_data from t_config_data_cache where data_flag = ? limit 1",
                    data_flag));
            // 有data_falg对应的数据
            if (null != cursor && cursor.moveToNext()) {
                int serverVer = cursor.getInt(0);
                int localVer = cursor.getInt(1);
                cachedData = cursor.getString(2);
                // 本地缓存版本为最新 ，且缓存数据不为空
                if (serverVer == localVer && !TextUtils.isEmpty(cachedData)) {
                    return cachedData;
                }
            }
        } catch (DbException e) {
            e.printStackTrace();
        }
        return cachedData;
    }

    public boolean updateCacheData(String data_flag, String cachData) {
        DbUtils dbUtils = db;
        Cursor cursor = null;
        try {
            dbUtils.createTableIfNotExist(ConfigDataCacheBean.class);
            // 取数据库data_flag对应的数据
            cursor = db.execQuery(new SqlInfo(
                    "select id,server_ver from t_config_data_cache where data_flag = ? limit 1", data_flag));
            ConfigDataCacheBean bean = new ConfigDataCacheBean();
            bean.data_flag = data_flag;
            bean.cache_data = cachData;

            if (null != cursor && cursor.moveToNext()) {
                bean.id = cursor.getInt(0);
                bean.server_ver = cursor.getInt(1);
                bean.local_ver = bean.server_ver;

                dbUtils.update(bean, WhereBuilder.b("id", "=", bean.id), "local_ver", "cache_data");
                return true;
            }

            //
            bean.server_ver = 1;
            bean.local_ver = 1;
            db.save(bean);
        } catch (DbException e) {
            e.printStackTrace();
        } finally {
            if (null != cursor) {
                cursor.close();
                cursor = null;
            }
        }
        return false;
    }

    /**
     * 配置数据缓存元
     */
    @Table(name = "t_config_data_cache")
    private static class ConfigDataCacheBean {
        int    id;        // 数据id
        String data_flag; // 数据缓存标识
        int    server_ver; // 服务器配置版本
        int    local_ver; // 本地缓存版本
        String cache_data; // 缓存数据内容
        int    is_valid;  // 数据是否生效：1生效，0不生效
    }
}
